package controlerLayer;
import java.util.ArrayList;

import modelLayer.Forest;
import modelLayer.ForestContainer;
import modelLayer.Tree;
/**
 * The Controller Class for the Forest Objects
 * 
 * @author Nick D. Pedersen 
 * @version 0.1
 */
public class ForestCtr
{
    private ForestContainer fCont;

    /**
     * Constructor for objects of class ForestCtr
     */
    public ForestCtr(){
        fCont = ForestContainer.getInstance();
    }
    
    /**
     * getForests - Returns the owners 
     * @Return String - String with the information about the owners
     */
    public String getForests(){
        String forests = "";
        ArrayList<Forest> fList = fCont.getContainer();
        if(!fList.isEmpty()){
            for(Forest f : fList){
                forests += f.getName() + ", ";
            }
            forests = forests.substring(0, forests.length()-2);
        }else{
            throw new NullPointerException("There are no Forests!");
        }
        return forests;
    } 
    
    public ArrayList<Forest> getUnOwned(){
    	ArrayList<Forest> ret = new ArrayList<Forest>();
    	for(Forest f : fCont.getContainer()){
    		if(f.getOwner() == null){
    			ret.add(f);
    		}
    	}
    	
    	return ret;
    }

    /**
     * addForest - Adds an forest by name, address and phoneNumber
     * @Param String name - The name of the forest
     * @Param String location - The location of the forest
     * @Param double squareMeter - The size of the forest in square meters
     * @Param double price - The price of the forest
     */
    public void addForest(String name, String location){
        fCont.addForest(new Forest(name, location));
    }
    
    /**
     * addForest - Adds an existing forest
     * @Param Forest f - The existing forest
     */
    public void addForest(Forest f){
        fCont.addForest(f);
    }
    
     /**
     * findForest - Finds a Forest by name
     * @Param String name - the name of the forest
     * @Return Forest - The Forest object
     */
    public Forest findForest(String name){
        Forest f = fCont.findForest(name);
        return f;
        
    }
    
     /**
     * addTreeToForest - Adds a Tree object to a Forest object
     * @Param String forestName - The name of the Forest object
     * @Param String sort - The sort / spicie of the Tree
     * @Param double lifespan - The lifespan of the Tree
     * @Param double costPerSquareKilometer - The cost per square kilometer of the tree
     * @Param double revenuePerSquareKilometer - The revenue per square kilometer of the tree
     */
    public void addTreeToForest(String forestName, String sort, double lifespan,double squareKilometer , double costPerSquareKilometer, double revenuePerSquareKilometer){
        Forest f = findForest(forestName);
        f.addTree(new Tree(sort, lifespan, squareKilometer, costPerSquareKilometer, revenuePerSquareKilometer, f));
    }
    
    
    public void moveTreeToForest(Tree t, Forest oldForest, Forest newForest){
    	oldForest.removeTree(t);
    	newForest.addTree(t);
    }
    
    /**
     * getTrees - Returns the information of all the trees owned by the Forest Object
     * @Param String name - name of the Forest object
     * @Return String - String with information of all the trees
     */
    public String getTrees(String name){
        String trees = "";
        Forest f = fCont.findForest(name);
        ArrayList<Tree> tList = f.getTrees();
        if(!tList.isEmpty()){
            for(Tree t : tList){
                trees += t.getSort() + ", ";
            }
            trees = trees.substring(0, trees.length()-2);
        }else{
            throw new NullPointerException("There are no trees in the given Forest");
        }
        
        return trees;
    }
    
    /**
     * getTreeAmount - Returns the amount of trees in Square Kilometers
     * @Param String forestName - The name of the forest to search.
     * @Param String treeSort - The name of the tree sort
     * @Return double - the amount in Square Kilometers
     */
    public double getTreeAmount(String forestName, String treeSort){
        Forest f = findForest(forestName);
        Tree t = f.findTree(treeSort);
        
        return t.getSquareKilometer();
    }

	public ArrayList<Forest> searchForest(String name) {
		ArrayList<Forest> forests = fCont.getContainer();
		ArrayList<Forest> ret = new ArrayList<Forest>();
		for(Forest f : forests){
			if(f.getName().toLowerCase().contains(name.toLowerCase())){
				ret.add(f);
			}
		}
		
		return ret;
	}

	public ArrayList<Forest> getAllForests() {
		return fCont.getContainer();
	}

	public void remove(Tree tree) {
		tree.getForest().removeTree(tree);
	}

	public void removeForest(Forest f) {
		fCont.remove(f);
	}
   
}
